package com.jhs.kjs.util;

import java.util.*;

public class DBUtil
{
	private static class CHARPair
	{
		char m_och = 0;
		char m_fch = 0;
		char m_tch = 0;
		CHARPair(char och, char fch, char tch)
		{
			this.m_och = och;
			this.m_fch = fch;
			this.m_tch = tch;	
		}
	}
	private static CHARPair[] s_charlist = {
		new CHARPair('ㄱ', '가', '깋')
		, new CHARPair('ㄲ', '까', '낗')
		, new CHARPair('ㄴ', '나', '닣')
		, new CHARPair('ㄷ', '다', '딯')
		, new CHARPair('ㄸ', '따', '띻')
		, new CHARPair('ㄹ', '라', '맇')
		, new CHARPair('ㅁ', '마', '밓')
		, new CHARPair('ㅂ', '바', '빟')
		, new CHARPair('ㅃ', '빠', '삫')
		, new CHARPair('ㅅ', '사', '싷')
		, new CHARPair('ㅆ', '싸', '앃')
		, new CHARPair('ㅇ', '아', '잏')
		, new CHARPair('ㅈ', '자', '짛')
		, new CHARPair('ㅉ', '짜', '찧')
		, new CHARPair('ㅊ', '차', '칳')
		, new CHARPair('ㅋ', '카', '킿')
		, new CHARPair('ㅌ', '타', '팋')
		, new CHARPair('ㅍ', '파', '핗')
		, new CHARPair('ㅎ', '하', '힣')
	};	
	private static Hashtable<Short, Short> s_charmap = new Hashtable<Short, Short>();	
	static 
	{
		for(int i = 0; i < s_charlist.length; i++ )
		{
			s_charmap.put((short)s_charlist[i].m_och, (short)i);
		}
	}

	public static String[] convKeyWord(String szKeyword)
	{
		StringBuffer fr = new StringBuffer();
		StringBuffer to = new StringBuffer();
		String[] ret = new String[2];
		if(null == szKeyword)
		{
			return null;
		}
		int i = 0;
		for( ; i < szKeyword.length(); i++ )
		{
			char ch = szKeyword.charAt(i);
			Short idx = s_charmap.get((short)ch);
			if(null != idx && (0 <= idx || idx < s_charlist.length))
			{
				CHARPair cpair = s_charlist[idx];	
				fr.append(cpair.m_fch);
				to.append(cpair.m_tch);
			}
			else
			{
				fr.append(ch);
				to.append(ch);
			}
		}
		to.append('힣');
		ret[0] = fr.toString();
		ret[1] = to.toString();
		return ret;	
	}

	public static String createSrchCondition(String szFieldNm, String szKeyword)
	{
		String[] keywords = convKeyWord(szKeyword);
		if(null == keywords)
		{
			return "";
		}
		if(null == szFieldNm)
		{
			szFieldNm = "";
		}
		StringBuffer szCond = new StringBuffer(); 
		if(!keywords[0].isEmpty())
		{
			szCond.append(szFieldNm)
				.append(" >= \'")
				.append(keywords[0])
				.append("\'");
		}	
		if(!keywords[1].isEmpty())
		{
			if(0 < szCond.length())
			{
				szCond.append(" and " );
			}
			szCond.append(szFieldNm)
				.append(" <= \'")
				.append(keywords[1])
				.append("\'");
		}	
		return szCond.toString();
	
	}


	/*public static void main(String[] argv)
	{
		System.out.println("가ㄱㅅ : " +  createSrchCondition("name", "가ㄱㅅ"));
	}*/
}

